/* <!--

    给定 nums = [2, 7, 11, 15], target = 9

    因为 nums[0] + nums[1] = 2 + 7 = 9
    所以返回 [0, 1]

 -->
 */
/* 方法一 暴力搜索 */
var nums = [2, 7, 11, 15]
function twoSum(arr, target) {
	for (var i = 0; i < arr.length; i++) {
		for (var j = i + 1; j < arr.length; j++) {
			if (arr[i] + arr[j] === target) {
				return [j, i]
			}
		}
	}
}
var result = twoSum(nums, 18)
console.log(result)

/* 方法二  hash */
var nums = [2, 7, 11, 15]
function twoSum(arr, target) {
	let obj = {}
	for (let i = 0; i < arr.length; i++) {
		if (obj[arr[i]] !== undefined) {
			return [obj[arr[i]], i]
		} else {
			obj[target - arr[i]] = i
		}
	}
}
var result = twoSum(nums, 9)
console.log(result)
